#!/usr/bin/env node

const http = require('http');

http.createServer((req,res)=>{
    res.setHeader('Access-Control-Allow-Origin', '*');//跨域策略的设置
    //1.parse url, get width and height
    // log(`${req.method} ${req.url} HTTP/${req.httpVersion}`);
    let url = new URL(req.url,`http://${req.headers.host}`);
    // const [width,height] = url.searchParams.values();
    const width = Number(url.searchParams.get('width'));
    const height = Number(url.searchParams.get('height'));
    // log(url);
    // log(width,height);

    //2.calculate area and permiter
    const rect = {
        area:width * height,
        perimeter: 2 * (width + height),
    };
    // log(rect);

    //3.respond JSON
    res.end(JSON.stringify(rect));
}).listen(3000);